import React from 'react'
import { connect } from 'dva'
import { Row,Col,Table } from 'antd'


/**
 * TraderIndexList 交易员指标列表
 */
const TraderIndexList = ({ 
    data 
   }) => {
    const height=document.body.clientHeight
    

    const fieldList = data.fieldList||[]
    const columns = []

    fieldList.filter(item => item.id === 'symbol').map(item => {
      columns.push({
        title: item.name,
        dataIndex: item.id,
        key: item.id,
        sorter: (a, b) => a[item.id] - b[item.id],
        width: 170,
        fixed: 'left'
      })
    })
    fieldList.filter(item => item.id !== 'symbol').map((item, index) => {
      columns.push({
        title: item.name,
        dataIndex: item.id,
        key: item.id,
        sorter: (a, b) => a[item.id] - b[item.id],
        width:170,
      })
    })
    const dataSource=data.list||[]
    
      return (
        <Table
          columns={columns}
          dataSource={dataSource}
          rowKey='symbol'
          pagination={false}
          scroll={{ x: columns.length*170, y: height-100 }}
        />
      )
   }

   

/**
 * [StockCollection 交易员汇总详情]
 * 引用组件 
 * TraderIndexList 交易员指标列表
 */
class StockCollection extends React.Component {
  state={
    data:this.props.stockIndex
  }

  componentDidMount() {
    this.fetchData(this.props)
  }
  componentWillReceiveProps(nextProps) {
    if((nextProps.begindate!==this.props.begindate)||(nextProps.enddate!==this.props.enddate)||(nextProps.trader!==this.props.trader)){
      this.fetchData(nextProps)
    }
    if(nextProps.stockIndex!==this.props.stockIndex){
      this.setState({
        data:nextProps.stockIndex
      })
    }
  }
  fetchData=(props)=>{

    this.props.dispatch({
      type: `dailyReplay/getStockIndexList`,
      payload:props
    })
  }
  render() {
    const { data } = this.state
    
    return (
      <Row>
        <Col>
          <TraderIndexList data={data} />
        </Col>
      </Row>
    )
  }
}

StockCollection.defaultProps={
  trader:'total',
  begindate: null,
  enddate: null,
  stockIndex:{}
}

export default connect()(StockCollection)
